package com.Offer;

import java.util.Scanner;

/*
    面试题43：1~n整数中1出现的次数
    题目描述：输入一个整数 n，求从 1 到整数 n 的十进制表示中 1 出现的次数
 */
public class demo43 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(NumberOf1Between1AndN_Solution(n));

    }

    public static int NumberOf1Between1AndN_Solution(int n){
        int count = 0;
        int i = 1;
        int current = 0,after = 0,before =0;
        while((n/i) != 0){
            before = n / (i*10);
            current = (n/i) % 10;
            after = n- (n/i) * i;

            if(current == 0){
                count = count + before*i;
            }else if(current == 1){
                count = count + before*i + after + 1;
            }else if(current > 1){
                count = count + (before+1)*i;
            }
            i = i*10;
        }
        return count;
    }

}
